import * as types from './mutation-types';
import { isExistence } from '../utils';
import {
  kpiEbcsCategoryCode,
  kpiUserinfo,
  kpiProductBrands,
  kpiUserTenant,
  kpiPageEemployer,
  kpiProductCategories
} from 'api';

export const dispatchCurrentUser = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.currentUser)) {
      resolve()
    } else {
      kpiUserinfo({}, that).then(res => {
        commit(types.SET_CURRENT_USER, res.data.data)
        resolve()
      })
      .catch(() => {
        reject()
      })
    }
  })
  return promise
}

export const dispatchCurrentTenant = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.currentTenant)) {
      resolve()
    } else {
      kpiUserTenant({}, that)
        .then(res => {
          commit(types.SET_CURRENT_TENANT, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchCertificateTypeEntries = function ({ commit, state }, { that }) {
  if (isExistence(state.certificateTypeEntries)) {
    return
  } else {
    let param = {
      categoryCode: 'CertificateType'
    }
    kpiEbcsCategoryCode(param, that).then(res => {
      commit(types.SET_CERTIFICATE_TYPE_ENTRIES, res.data.data)
    })
  }
}

export const dispatchPolicyStatusEntries = function ({ commit, state }, { that }) {
  if (isExistence(state.policyStatusEntries)) {
    return
  } else {
    let Payload = [
      { key: '100', value: '初始' },
      { key: '101', value: '待审核' },
      { key: '102', value: '已退回' },
      { key: '103', value: '审核通过' },
      { key: '107', value: '已退保' },
      { key: '109', value: '已完成' }
    ]
    commit(types.SET_POLICY_STATUS_ENTRIES, Payload)
  }
}

export const dispatchCoverageUnitEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.coverageUnitEntries)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'CoverageUnit'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          commit(types.SET_COVERAGE_UNIT_ENTRIES, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchPeriodUnitEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.periodUnitEntries)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'PeriodUnit'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          // console.log('期限单位: ', res)
          commit(types.SET_PERIOD_UNIT_ENTRIES, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchSaveMethodEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.saveMethodEntries)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'SaveMethod'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          // console.log('保单类名: ', res)
          res.data.data.push({key: 'piccNewClient', value: '疫苗险新接口'});
          const data = res.data.data;
          commit(types.SET_SAVE_METHOD_ENTRIES, data);
          resolve();
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchInsuredTypeEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.insuredTypeEntries)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'InsuredType'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          // console.log('被保险人类型: ', res)
          commit(types.SET_INSURED_TYPE_ENTRIES, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchPaymentControlEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.paymentControlEntries)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'PaymentControl'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          // console.log('支付类型: ', res)
          commit(types.SET_PAYMENT_CONTROL_ENTRIES, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchBeneficiaryTypeEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.beneficiaryTypeEntries)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'BeneficiaryType'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          // console.log('受益人类型: ', res)
          commit(types.SET_BENEFICIARY_TYPE_ENTRIES, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchCategoryEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.categoryEntries)) {
      resolve()
    } else {
      kpiProductCategories({}, that)
        .then(res => {
          console.log('险类: ', res)
          commit(types.SET_CATEGORY_ENTRIES, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchPolicyTypeEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.policyTypeEntries)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'PolicyType'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          // console.log('保单类型: ', res)
          commit(types.SET_POLICY_TYPE_ENTRIES, res.data.data)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

/**
 * @param {*}
 * @param {*}
 * 更改第三方支付平台开关标识
 */
export const dispatchThirdPayments = function ({ commit, state }, { that }) {
  return new Promise((resolve, reject) => {
    if (isExistence(state.productBrands)) {
      resolve()
    } else {
      let param = {
        categoryCode: 'thirdPayment'
      }
      kpiEbcsCategoryCode(param, that)
        .then(res => {
          commit(types.SET_THIRD_PAYMENTS, res.data.data)
          resolve()
        })
        .catch(err => {
          reject(err)
        })
    }
  })
}

// /**
//  * @returns 增加支付成功页面链接配置
//  */
// export const dispatchPayReturnUrls = function ({ commit, state }, { that }) {
//   return new Promise((resolve, reject) => {
//     if (isExistence(state.productBrands)) {
//       resolve()
//     } else {
//       let param = {
//         categoryCode: 'payReturnUrl'
//       }
//       kpiEbcsCategoryCode(param, that)
//         .then(res => {
//           commit(types.SET_RETURN_URLS, res.data.data)
//           resolve()
//         })
//         .catch(err => {
//           reject(err)
//         })
//     }
//   })
// }

export const dispatchProductBrands = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.productBrands)) {
      resolve()
    } else {
      kpiProductBrands({}, that)
        .then(res => {
          // console.log('品牌: ', res)
          commit(types.SET_PRODUCT_BRANDS, res.data.data)
          resolve(res)
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}

export const dispatchEmployerEntries = function ({ commit, state }, { that }) {
  let promise = new Promise(function (resolve, reject) {
    if (isExistence(state.employerEntries)) {
      resolve()
    } else {
      let param = {
        status: 'approved'
      }
      kpiPageEemployer(param, that)
        .then(res => {
          console.log('企业列表:', res)
          commit(types.SET_EMPLOYER_ENTRIES, res.data.data.list)
          resolve()
        })
        .catch(() => {
          reject()
        })
    }
  })
  return promise
}
